package com.controller;

import com.dto.BaseDto;
import com.entity.Goodtype;
import com.page.PageInfo;
import com.service.GoodtypeService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.Optional;

@Controller
public class GoodtypeController {
    @Resource
    GoodtypeService goodtypeService;

//传统传参，不需要绑定参数 url直接在后面写上？adid=0001
    @RequestMapping("/goodtype/getByAdidJson")
    @ResponseBody
    public Goodtype getByGtidJson(String gtid) {
        return goodtypeService.getByGtid(gtid);
    }

//rest风格传参 将参数变为url的一部分但是需要绑定 例如 /goodtype/getByAdidNew/0001
    @RequestMapping("/goodtype/getByGtidNewJSon/{gtid}")
    @ResponseBody
    public Goodtype getByGtidNewJson(@PathVariable("gtid") String gtid) {
        return goodtypeService.getByGtid(gtid);
    }

    @RequestMapping("/goodtype/getAllJson")
    @ResponseBody
    public BaseDto<Goodtype> getAllJson() {
        return goodtypeService.getAll();

    }

    @RequestMapping("/goodtype/getAll")
    public ModelAndView getAll() {
       BaseDto<Goodtype> dto = goodtypeService.getAll();
        return new ModelAndView("/back/goodtype/list","dto",dto);

    }

    @RequestMapping("/goodtype/getAllByPage")
    public ModelAndView getAllByPage(PageInfo pageInfo) {
        //页面传递的pageInfo可能为空或者没有值，进行判空处理
        Optional<PageInfo> pageInfoOptional = Optional.ofNullable(pageInfo);
        pageInfo = pageInfoOptional.isPresent()?pageInfo:new PageInfo();
        BaseDto<Goodtype> dto = goodtypeService.getAllByPage(pageInfo);
        ModelAndView mv = new ModelAndView("/back/goodtype/list","dto",dto);
        return mv;
    }
    @RequestMapping("/goodtype/getAllByPageJson")
    @ResponseBody
    public BaseDto getAllByPageJson(PageInfo pageInfo) {
        //页面传递的pageInfo可能为空或者没有值，进行判空处理
        Optional<PageInfo> pageInfoOptional = Optional.ofNullable(pageInfo);
        pageInfo = pageInfoOptional.isPresent()?pageInfo:new PageInfo();
        BaseDto<Goodtype> dto = goodtypeService.getAllByPage(pageInfo);
        return dto;
    }


    @RequestMapping("/goodtype/searchNameByPage/{adname}")
    @ResponseBody
    public BaseDto searchNameByPage(PageInfo pageInfo,@PathVariable("adname")String adname) {
        //页面传递的pageInfo可能为空或者没有值，进行判空处理
        Optional<PageInfo> pageInfoOptional = Optional.ofNullable(pageInfo);
        pageInfo = pageInfoOptional.isPresent()?pageInfo:new PageInfo();
        BaseDto<Goodtype> dto = goodtypeService.searchNameByPage(pageInfo,adname);
        return dto;
    }


    /**
     * 根据主键查询
     * @param gtid
     * @return
     */
    @RequestMapping("/goodtype/getGoodtypeByGtid/{gtid}")
    @ResponseBody
    public BaseDto getGoodtypeByGtid(@PathVariable("gtid") String gtid){
        return goodtypeService.getGoodtypeByGtid(gtid);
    }

    /**
     * 添加一个实体
     * @param goodtype
     * @return
     */
    @RequestMapping("/goodtype/addGoodtype")
    @ResponseBody
    public BaseDto addGoodtype(Goodtype goodtype){
        return goodtypeService.addGoodtype(goodtype);
    }
    /**
     * 修改实体
     */
    @RequestMapping("/goodtype/updateGoodtype")
    @ResponseBody
    public BaseDto updateGoodtype(Goodtype goodtype){
        return goodtypeService.updateGoodtype(goodtype);
    }
    public BaseDto updateGoodtypeByExample(){
        return null;
    }

    /**
     * 删除一个实体
     * @param gid
     * @return
     */
    @RequestMapping("/goodtype/deleteGoodtypeByPrimayKey/{gid}")
    @ResponseBody
    public BaseDto deleteGoodtypeByPrimayKey(@PathVariable("gid") String gid){
        return goodtypeService.deleteGoodtypeByPrimayKey(gid);
    }

    /**
     * 删除实体根据示例
     * @return
     */
    public BaseDto deleteGoodtypeByExample(){
        return null;
    }
}
